package cn.com.adcc.utils;

import org.joda.time.DateTime;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Created by anzilu on 2015/10/9.
 * update by hht 2015/10/15
 */
public class Traversal {

    private  ArrayList<File> filelist = new ArrayList<File>();

    private long counter=0;

    public  ArrayList<File> getFilelist() {
        return filelist;
    }

    public  long getCounter() {
        return counter;
    }
    public static void main(String[] args) {
        long a = System.currentTimeMillis();
        Traversal traversal=new Traversal();
        traversal.refreshFileList("D:\\MSVSWorkspace");
        System.out.println(System.currentTimeMillis() - a);
        System.out.println(traversal.counter);
    }

    public void refreshFileList(String strPath) {


        File dir = new File(strPath);
        File[] files = dir.listFiles();
        if (files == null)
            return;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                refreshFileList(files[i].getAbsolutePath());
            } else {
                if(files[i].getPath().endsWith(".temp"))continue;
                counter++;
                filelist.add(files[i]);
            }
        }

        Collections.sort(filelist, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                if(o1.lastModified()-o2.lastModified()>0)
                    return -1;
                else if(o1.lastModified()-o2.lastModified()==0)
                    return 0;
                else
                    return 1;
//                return o1.lastModified()- o2.lastModified()>0?-1:1;
            }
        });
    }
}
